const fs = require('fs')
const ejs = require('ejs')
import { ObjectId, MongoClient } from 'mongodb'
import YAML from 'yamljs'
import {formatDate} from '../utils/date'
import BaseResult from '../lib/BaseResult'

const { mongo } = YAML.parse(fs.readFileSync('./server/config.yaml').toString());
const url = mongo.host;
import mail from '../lib/mail'
const logMailTpl = fs.readFileSync('./server/view/log-mail.ejs').toString();
// const io = require('socket.io')
console.log(mail)
// var EventEmitter = require('events').EventEmitter; 
// var event = new EventEmitter(); 
import test from './test'
class LogController {
    static async send(ctx) {
        let data = ctx.request.body;
        // let html =[];
        // for(let i=0;i<data.msg.length;i++){
        //     html.push(`<b>${data.msg[i].msg}</b>`)
        //     html.push(`<b>${data.msg[i].time}</b>`)
        // }
        data.date = formatDate;
        ejs.renderFile('./server/view/log-mail.ejs', data, {}, function(err, str){
            // str => 输出绘制后的 HTML 字符串
            mail.send(str);
            ctx.body =str
        });
        // let html = ejs.render(logMailTpl,data);
        // mail.send(html);
        
    }
    static async  listen(ctx){
        // test.on('listen', function() { 
        //     console.log('static listen 88 事件触发'); 
        // });
        setInterval(()=>{
            test.emit()
        },1000)
        console.log(test.emit())
    //     test.emit('listen'); 
    //    console.log('listen')
       // ctx.socket.emit('receiveMsg',{a:'ctx.socket'})
        //io.of
        // io.sockets.emit
       // console.log(io)
      //console.log(ctx.io)
    //   console.log('*************************************')
    //  console.log(ctx.socket)
    //  console.log('*************************************')
    //  console.log(ctx.req.io)
    //  console.log('*************************************')
    //  console.log(ctx.res.io)
   //  console.log('*************************************')
     //console.log(ctx.app)
     // let y =  ctx.socket
     //ctx.response.socket.emit('receiveMsg',{a:'receiveMsg'})
      
      //  io.sockets.emit('receiveMsg',{a:'receiveMsg'})
        ctx.body = 'dasd';
    }
}
export default LogController;